package com.weilele.leisure.time.ui.holder

import android.view.View
import com.google.android.exoplayer2.offline.Download
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.databinding.HolderCacheListItemHolderBinding
import com.weilele.mvvm.adapter.MvvmHolder
import com.weilele.mvvm.adapter.mvvmListAdapter
import com.weilele.mvvm.adapter.mvvmRcvAdapter
import com.weilele.mvvm.utils.activity.invisible
import com.weilele.mvvm.utils.activity.visible
import com.yizisu.playerlibrary.service.download.PlayerDownload


class CacheListItemHolder : MvvmHolder<CacheListItemHolder.Bean, HolderCacheListItemHolderBinding> {
    companion object {
        fun toAdapter() = mvvmRcvAdapter(CacheListItemHolder::class.java)
    }

    constructor(binding: HolderCacheListItemHolderBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    data class Bean(val video: VideoDetailBean.PlayersDTO.DatasDTO, var cache: Download?)

    override fun bindData(data: Bean) {
        val cache = data.cache
        mBinding.text.text = data.video.text
        if (cache == null) {
            mBinding.ivStatus.invisible()
        } else {
            showDownloadInfo(cache)
        }
    }

    fun showDownloadInfo(cache: Download?) {
        mBinding.ivStatus.visible()
        if (PlayerDownload.isDownloadCompleted(cache)) {
            mBinding.ivStatus.setImageResource(R.drawable.icon_downloaded)
        } else {
            mBinding.ivStatus.setImageResource(R.drawable.icon_downloading)
        }
    }
}